<template>
  <van-icon
    :name="value === 1 ? 'good-job' : 'good-job-o'"
    :color="value === 1 ? 'red' : ''"
    @click="likeBtn"
    :loading="loading"
  ></van-icon>
</template>

<script>
import { likeArticle, cancelLikeArticle } from '@/api/article.js'
export default {
  name: 'likeArticle',
  data() {
    return {
      loading: false
    }
  },
  props: {
    value: {
      type: Number,
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },
  methods: {
    async likeBtn() {
      try {
        let status = -1
        this.loading = true
        if (this.value === 1) {
          // 已点赞 取消点赞
          await cancelLikeArticle(this.articleId)
        } else {
          // 未点赞 点赞
          await likeArticle(this.articleId)
          status = 1
        }
        this.$emit('input', status)
        this.$toast.success(status === 1 ? '点赞成功' : '取消点赞')
      } catch (err) {
        this.$toast.fail('操作失败')
      }
      this.loading = false
    }
  }
}
</script>

<style scoped lang="less"></style>
